﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.Model;
using OgilvyOne.BLL;

public partial class DataBaseManage_ModelAdd : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string layerid = Request["layerid"];
            PopedomValidate(layerid, QUERY);
            this.headers.Text = new ColumnBll().LayeridSysColumnNaiv(Layerid);//系统
            string url = "model_add.aspx?tid=" + Request.Params["tid"] + "&layerid=" + Request.Params["layerid"];
            this.headers.Text += "<img src='../images/subnav_dot.gif' width='9' height='9' />&nbsp;&nbsp;<a href='" + url + "'>模板详细信息</a>&nbsp;&nbsp";
            string tid = Request.Params["tid"];
            if (tid != null && tid != "")
            {
                ModelManageBLL mmb = new ModelManageBLL();
                ModelManageModel mmm = mmb.getModel(int.Parse(tid));
                this.TextBox1.Text = mmm.TemName;
                /*
                * 代码修改
                * 修改日期：2012-01-15
                * 修改人员：刘扬
                * 修改原因：该变量仅为当前页面使用，故不需要使用Session进行保存，改为使用ViewState进行保存
                * */
                //Session["temname"] = mmm.TemName;
                ViewState["temname"] = mmm.TemName;

                this.DropDownList1.Text = mmm.ItemID;
                this.TextBox2.Text = mmm.TemCotent;
                if (mmm.Isdel == 0)
                {
                    this.CheckBox1.Checked = true;
                }
                else
                {
                    this.CheckBox1.Checked = false;
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string tid = Request["tid"];
        if (tid != "" && tid != null)
        {
            if (this.TextBox1.Text.ToString().Trim() == ViewState["temname"].ToString().Trim())
            {
                string layerid = Request["layerid"];
                PopedomValidate(layerid, MOD);
                update();
            }
            else
            {
                if (JudgmentRepeat() == 0)
                {
                    string layerid = Request["layerid"];
                    PopedomValidate(layerid, MOD);
                    update();
                }
                else
                {
                    Response.Write("<script>alert('名称重复!请重新输入!')</script>");
                }
            }
        }
        else
        {
            if (JudgmentRepeat() == 0)
            {
                string layerid = Request["layerid"];
                PopedomValidate(layerid, ADD);
                insert();
            }
            else
            {
                Response.Write("<script>alert('名称重复!请重新输入!')</script>");
            }
        }
    }

    private void insert()
    {
        ModelManageBLL addmode = new ModelManageBLL();
        ModelManageModel addmodelinfo = new ModelManageModel();
        addmodelinfo.TemName = this.TextBox1.Text.ToString().Trim();
        addmodelinfo.ItemID = this.DropDownList1.Text.ToString().Trim();
        addmodelinfo.TemCotent=this.TextBox2.Text.ToString().Trim();
        if (CheckBox1.Checked == true)
        {
            addmodelinfo.Isdel = 0;
        }
        else
        {
            addmodelinfo.Isdel = 1;
        }


        int ts = addmode.AddModel(addmodelinfo);
        if (ts > 0)
        {
            Response.Redirect("model_manage.aspx?layerid=" + Request.Params["layerid"]);
            return;
        }
        else
        {
            //Response.Redirect("log_delete.aspx");
        }
    }

    private void update()
    {
        try
        {
            string tid = Request["tid"];
            ModelManageBLL mmb=new ModelManageBLL();
            ModelManageModel mmm=new ModelManageModel();
            mmm.TmenID=int.Parse(tid);
            mmm.ItemID=this.DropDownList1.Text.ToString().Trim();
            mmm.TemName=this.TextBox1.Text.ToString().Trim();
            mmm.TemCotent=this.TextBox2.Text.ToString().Trim();
            if(CheckBox1.Checked==true)
            {
                mmm.Isdel=0;
            }
            else
            {
                mmm.Isdel=1;
            }
            int ts = mmb.updateModel(mmm);
            if (ts > 0)
            {
                Response.Redirect("model_manage.aspx?layerid=" + Request.Params["layerid"]);
                return;
            }
        }
        catch
        { }
    }

    protected void Button2_Click1(object sender, EventArgs e)
    {
       this.TextBox1.Text = "";
        this.TextBox2.Text = "";
        this.CheckBox1.Checked = false;
    }
    protected void Button3_Click1(object sender, EventArgs e)
    {
        Response.Redirect("model_manage.aspx?layerid=" + Request.Params["layerid"]);
        return;
    }

    private int JudgmentRepeat()
    {
        string strWhere = this.TextBox1.Text.ToString().Trim();
        ModelManageBLL mmb = new ModelManageBLL();
        DataSet ds = mmb.GetModel(" tem_name = '" + strWhere + "'");
        if (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0)
        {
            return 0;
        }
        else
        {
            return 1;
        }

    }
}
